package com.dcy.znoa.controller.sys;

import com.alibaba.fastjson.JSON;
import com.dcy.znoa.common.annotation.AuthenticationAPI;
import com.dcy.znoa.common.controller.BaseController;
import com.dcy.znoa.common.exception.BusinessException;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.sys.model.roleAuth.AddRoleAuthModel;
import com.dcy.znoa.controller.sys.model.roleAuth.SelectRoleAuthModel;
import com.dcy.znoa.domain.sys.RoleAuth;
import com.dcy.znoa.service.sys.RoleAuthService;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

/**
 * @author chenjie
 */
@Controller
@RequestMapping("roleAuth")
public class RoleAuthController extends BaseController {
    @Autowired
    RoleAuthService roleAuthService;

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/insert")
    //可批量插入
    public JsonCommon insert(@Valid AddRoleAuthModel model,@RequestParam String urlIdsJsonStr) throws BusinessException, InvocationTargetException, IllegalAccessException {
        
    	List<Long> idsList = JSON.parseArray(urlIdsJsonStr,Long.class);
    	if (!idsList.isEmpty()) {
    		for (Long urlId : idsList) {
    			RoleAuth roleAuth = new RoleAuth();
                BeanUtils.copyProperties(roleAuth, model);
                roleAuth.setCreateOperId(getCurrentUser().getOperId());
                roleAuth.setUrlId(urlId);;
                roleAuthService.insert(roleAuth);
			}    		          
		}
        
        return new JsonCommon(JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/delete")
    //可批量物理删除
    public JsonCommon delete(@RequestParam Long roleId ,@RequestParam String urlIdsJsonStr) throws BusinessException, InvocationTargetException, IllegalAccessException {           	
    	List<Long> idsList = JSON.parseArray(urlIdsJsonStr,Long.class);
    	if (!idsList.isEmpty()) {
    		 return roleAuthService.delete(roleId ,idsList);	          
		}else {
			return new JsonCommon(JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
		}
       
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/update")
    //编辑角色权限：先删除原有权限，然后增加权限
    public JsonCommon update(@RequestParam Long roleId ,@RequestParam String urlIdsJsonStr) throws BusinessException, InvocationTargetException, IllegalAccessException {           	
    	//先删除原有权限
    	roleAuthService.deleteByRoleId(roleId);	
    	
    	//然后增加权限
    	List<Long> idsList = JSON.parseArray(urlIdsJsonStr,Long.class);
    	if (!idsList.isEmpty()) {
    		Date createTime = new Date();
    		for (Long urlId : idsList) {
    			RoleAuth roleAuth = new RoleAuth();
    			roleAuth.setRoleId(roleId);
                roleAuth.setCreateOperId(getCurrentUser().getOperId());
                roleAuth.setCreateTime(createTime);
                roleAuth.setUrlId(urlId);
                roleAuth.setValidFlag(1);
                roleAuthService.insert(roleAuth);
			}           
		}

		return new JsonCommon(JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
       
    }
}
